package com.renren.api.connect.android.demo.activity;

import java.util.List;

import com.renren.api.connect.android.AsyncRenren;
import com.renren.api.connect.android.Renren;
import com.renren.api.connect.android.common.AbstractRequestListener;
import com.renren.api.connect.android.demo.R;
import com.renren.api.connect.android.exception.RenrenError;
import com.renren.api.connect.android.photos.AlbumBean;
import com.renren.api.connect.android.photos.AlbumGetRequestParam;
import com.renren.api.connect.android.photos.PhotoBean;
import com.renren.api.connect.android.photos.PhotoGetRequestParam;
import com.renren.api.connect.android.photos.PhotoGetResponseBean;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class PhotoListActivity extends BaseActivity {
	PhotoGetRequestParam photoGetRequestParam = new PhotoGetRequestParam();
	private static final String TAG = "PhotoListActivity";
	LinearLayout photosGetLayout;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		ScrollView main = (ScrollView) LayoutInflater.from(
				this.getBaseContext())
				.inflate(R.layout.photo_get_layout, null);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.FILL_PARENT,
				LinearLayout.LayoutParams.FILL_PARENT);
		root.addView(main, layoutParams);
		
		//setContentView(R.layout.albums_get_layout);
		// 初始化标题文字
		AlbumBean album = getIntent().getParcelableExtra("album");
		titlebarText.setText(album.getName());
		
		photoGetRequestParam.setAid(album.getAid());
		photoGetRequestParam.setUid(album.getUid());
		photosGetLayout = (LinearLayout) findViewById(R.id.main_layout);
		AsyncRenren task = new AsyncRenren(renren);
		task.getPhotos(photoGetRequestParam, new AbstractRequestListener<PhotoGetResponseBean>() {

			@Override
			public void onComplete(final PhotoGetResponseBean bean) {
				// TODO Auto-generated method stub
				PhotoListActivity.this.runOnUiThread(new Runnable() {
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Log.d(TAG, "onComplete: " + bean.toString());
						List<PhotoBean> photos = bean.getPhotos();
						LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
						Log.d(TAG, "photo cnt: " + photos.size());
						for (final PhotoBean photo: photos) {
							Log.d("", "--------------" + photo.getUrlTiny());
							TextView tv = new TextView(PhotoListActivity.this);
							tv.setText(photo.getFileName());
							photosGetLayout.addView(tv, params);
							final ImageView iv = new ImageView(PhotoListActivity.this);
							iv.setBackgroundColor(Color.RED);
							iv.setMinimumHeight(20);
							iv.setMinimumWidth(20);
							iv.setOnClickListener(new View.OnClickListener() {
								@Override
								public void onClick(View v) {
									Intent intent = new Intent(PhotoListActivity.this, PhotoListActivity.class);
									intent.putExtra(Renren.RENREN_LABEL, renren);
									intent.putExtra("album", photo);
									startActivity(intent);
								}
							});
							photosGetLayout.addView(iv, params);
							photosGetLayout.requestLayout();
							ImageLoader.LoadedCallback callback = new ImageLoader.LoadedCallback() {
								
								@Override
								public void onBitmapLoaded(Bitmap loadedBitmap) {
									Log.d(TAG, loadedBitmap.getWidth() + " * " + loadedBitmap.getHeight());
									iv.setImageBitmap(loadedBitmap);
								}
							};
							ImageLoader loader = new ImageLoader(PhotoListActivity.this, mHandler);
							loader.getBitmap(photo.getUrlMain(), callback, false);
					}
				}
			});
			}

			@Override
			public void onFault(Throwable fault) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onRenrenError(RenrenError renrenError) {
				// TODO Auto-generated method stub
				
			}
			
		});
	}
	Handler mHandler = new Handler();
}
